//
//  ZDDUserAccountTools.swift
//  小蜗牛_新浪微博
//
//  Created by 周冬冬 on 16/6/26.
//  Copyright © 2016年 我的mac. All rights reserved.
//

import UIKit

class ZDDUserAccountTools: NSObject {

    //路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    //保存的用户歇息数据对象
    var userAccount:ZDDUserAccount?
    
    //判断是否登录
    var islogin :Bool {
    
    return accessToken != nil
    
    }
    
    //访问令牌
    var accessToken:String? {
    
        if userAccount?.access_token == nil {
        
        
            return nil
        
        }else{
            if userAccount?.expires_Date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            return userAccount?.access_token
            }else{
            
                return nil
            
            }
        
        }
    
    
    }
    
    static let sharedTools:ZDDUserAccountTools = ZDDUserAccountTools()
    
    override init() {
        super.init()
        
        userAccount = getUserAccount()
    }
    
    //保存对象
    func saveUserAccount(userAccount:ZDDUserAccount) {
    
    self.userAccount = userAccount
        
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    
    
    }
    
    //获取对象
    func getUserAccount() ->ZDDUserAccount?{
    
    let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? ZDDUserAccount
        
        return result
    
    
    
    }
    
}


















